<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="挂账单位名字" prop="accountUnitName">
      <el-input v-model="dataForm.accountUnitName" placeholder="挂账单位名字"></el-input>
    </el-form-item>
    <el-form-item label="单位电话" prop="accountUnitTel">
      <el-input v-model="dataForm.accountUnitTel" placeholder="单位电话"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
		<el-radio v-model="dataForm.status" label="1">启用</el-radio>
		<el-radio v-model="dataForm.status" label="2">关闭</el-radio>
    </el-form-item>
    <el-form-item label="备注" >
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
				disabledSubmit:false,
        visible: false,
        dataForm: {
          accountUnitId: 0,
          accountUnitName: '',
          accountUnitTel: '',
          tel: '',
          sellerId: '',
          totalAccountNum: '',
          notSettlementNum: '',
          notSettlementTotal: '',
          status: '1',
          remark: ''
        },
        dataRule: {
          accountUnitName: [
            { required: true, message: '挂账单位名字不能为空', trigger: 'blur' }
          ],
          accountUnitTel: [
            { required: true, message: '单位电话不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '所属租户不能为空', trigger: 'blur' }
          ],
          totalAccountNum: [
            { required: true, message: '总挂单数不能为空', trigger: 'blur' }
          ],
          notSettlementNum: [
            { required: true, message: '未结款单数不能为空', trigger: 'blur' }
          ],
          notSettlementTotal: [
            { required: true, message: '未结款总额不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(1启用2关闭)不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.accountUnitId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.accountUnitId) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstoreaccountunit/info/${this.dataForm.accountUnitId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.accountUnitName = data.tStoreAccountUnit.accountUnitName
                this.dataForm.accountUnitTel = data.tStoreAccountUnit.accountUnitTel
                this.dataForm.tel = data.tStoreAccountUnit.tel
                this.dataForm.sellerId = data.tStoreAccountUnit.sellerId
                this.dataForm.totalAccountNum = data.tStoreAccountUnit.totalAccountNum
                this.dataForm.notSettlementNum = data.tStoreAccountUnit.notSettlementNum
                this.dataForm.notSettlementTotal = data.tStoreAccountUnit.notSettlementTotal
                this.dataForm.status = data.tStoreAccountUnit.status.toString()
                this.dataForm.remark = data.tStoreAccountUnit.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstoreaccountunit/${!this.dataForm.accountUnitId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'accountUnitId': this.dataForm.accountUnitId || undefined,
                'accountUnitName': this.dataForm.accountUnitName,
                'accountUnitTel': this.dataForm.accountUnitTel,
                'tel': this.dataForm.tel,
                'sellerId': this.dataForm.sellerId,
                'totalAccountNum': this.dataForm.totalAccountNum,
                'notSettlementNum': this.dataForm.notSettlementNum,
                'notSettlementTotal': this.dataForm.notSettlementTotal,
                'status': this.dataForm.status.toString(),
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
